using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using CommLayer;
using Tamir.IPLib;
using Tamir.IPLib.Packets;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using System.Diagnostics;

namespace AssetCapture
{
    class AssetFunction
    {
        static StringComInt m_DeckConnection1 = null;
        static StringComInt m_DeckConnection2 = null;
        public string m_DeckResponse="";
        public bool Copying = false;

        const string PRI_DECK_IPADDRESS = "192.168.20.21";
        const string BKU_DECK_IPADDRESS = "192.168.20.22";
        const int DECK_PORT = 49152;

        /// <summary>
        /// Frame calculating for different set of channel
        /// </summary>
        /// <param name="imageWidth"></param>
        /// <param name="imageHight"></param>
        /// <param name="left_STW"></param>
        /// <param name="left_STH"></param>
        /// <param name="left_LW"></param>
        /// <param name="left_LH"></param>
        /// <param name="right_STW"></param>
        /// <param name="right_STH"></param>
        /// <param name="right_LW"></param>
        /// <param name="right_LH"></param>
        /// <param name="RectHight"></param>
        public void GetFrameAtt(double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int right_STW, out int right_STH, out int right_LW, out int right_LH, out int RectHight)
        {
            double w1 = (double)256 / (double)479;
            double w2 = (double)223 / (double)479;
            double h1 = (double)2160 / (double)4311;
            double h2 = (double)2151 / (double)4311;
            double Rect = (double)3832 / imageWidth * imageHight;

            RectHight = Convert.ToInt32(Rect);

            left_STW = 0;
            left_STH = Convert.ToInt32(imageHight / (double)2 * h2);
            left_LW = Convert.ToInt32(w1 * imageWidth);
            left_LH = Convert.ToInt32(h1 * imageHight);

            right_STW = left_LW;
            right_STH = left_STH;
            right_LW = Convert.ToInt32(w2 * imageWidth);
            right_LH = left_LH;

            if (RectHight > 1080)
            {
                RectHight = 1080;
            }
            else
            {
                left_STW = 0;
                left_STH = 0;
                left_LH = (int)imageHight;

                right_STH = 0;
                right_LH = left_LH;
            }
        }

        /// <summary>
        /// Frame Calculation for Ticker
        /// </summary>
        /// <param name="imageWidth"></param>
        /// <param name="imageHeight"></param>
        /// <param name="RectHight"></param>
        public void GetTickerFrame(double imageWidth, double imageHeight, out int RectHight)
        {
            double Rect = (double)2048 / imageWidth * imageHeight;

            RectHight = Convert.ToInt32(Rect);

            if (RectHight > 1080)
                RectHight = 1080;
        }

        /// <summary>
        /// For the TCP connection 
        /// </summary>
        public void TCPIni()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(SetDeckAddress));
        }

        /// <summary>
        /// Initialize the video input connection
        /// </summary>
        /// <param name="dumpOb"></param>
        private void SetDeckAddress(object dumpOb)
        {
            m_DeckConnection1 = new StringComTcpIp();
            m_DeckConnection1.IPAddress = PRI_DECK_IPADDRESS;
            m_DeckConnection1.Port = DECK_PORT;

            m_DeckConnection2 = new StringComTcpIp();
            m_DeckConnection2.IPAddress = BKU_DECK_IPADDRESS;
            m_DeckConnection2.Port = DECK_PORT;

            m_DeckConnection1.InitializeClient(logStringConnect, logString, logStringException);
            m_DeckConnection2.InitializeClient(logStringConnect, logString, logStringException);

            SendRequest("Source01");
        }

        public void SendRequest(string Command)
        {
            m_DeckResponse = "";
            m_DeckConnection1.QueueSendString(Command);
            m_DeckConnection2.QueueSendString(Command);
        }

        // report back
        public void logString(DateTime time, string str)
        {
            m_DeckResponse = str;
        }

        public static void logStringConnect(DateTime time, string str)
        {
            // create a writer and open the file

            string timemsg = time.Ticks.ToString();
            //if (str.Length > 2048)
            //{
            //    TextWriter tw = new StreamWriter("c:\\temp\\magtest" + timemsg + ".txt");
            //    // write a line of text to the file
            //    tw.Write(str);
            //    //// close the stream
            //    tw.Close();
            //}

            Console.WriteLine("{0}:{1}:{2},{3} , {4}",
                time.Hour, time.Minute, time.Second, time.Millisecond, str.Length > 2048 ? "long str" : str);


        }
        public static void logStringException(DateTime time, string str)
        {
            // create a writer and open the file
            string timemsg = time.Ticks.ToString();
            if (str.Length > 2048)
            {
                //TextWriter tw = new StreamWriter("c:\\temp\\magtest" + timemsg + ".txt");
                //// write a line of text to the file
                //tw.Write(str);
                ////// close the stream
                //tw.Close();
            }

            Console.WriteLine("{0}:{1}:{2},{3} , {4}",
                time.Hour, time.Minute, time.Second, time.Millisecond, str.Length > 2048 ? "long str" : str);

        }

        /// <summary>
        /// Copy the Ingest file for batch 
        /// </summary>
        /// <param name="fileCopy"></param>
        public void IngestFileCopy(object fileCopy)
        {
            Copying = true;

            string[] fCopy = (string[])fileCopy;

            File.Copy(fCopy[0], fCopy[1], true);

            FileInfo fInfo = new FileInfo(fCopy[0]);

            Copying = false;
            MessageBox.Show("\"" + fInfo.Name + "\" has been scheduled for later ingestion when the Application is idle");
        }

        /// <summary>
        /// Copy the Folder for Batch 
        /// </summary>
        /// <param name="folderCopy"></param>
        public void IngestFolderCopy(object folderCopy)
        {
            Copying = true;

            string[] fCopy = (string[])folderCopy;

            if (!Directory.Exists(fCopy[1]))
                Directory.CreateDirectory(fCopy[1]);
            
            DirectoryInfo dir = new DirectoryInfo(fCopy[0]);

            FileInfo[] fInfo = dir.GetFiles();

            foreach (FileInfo fI in fInfo)
            {
                File.Copy(fI.FullName, fCopy[1] + "\\" + fI.Name);
            }
            Copying = false;
            MessageBox.Show("\"" + dir.Name + "\" has been scheduled for later ingestion when the Application is idle");
        }

        /// <summary>
        /// Auto kill and start process for Autoingest and thumbnail
        /// </summary>
        public void autoProcess()
        {
            Process[] prs = Process.GetProcesses();
            foreach (Process pr in prs)
            {
                if (pr.ProcessName == "ThumbnailVideo" || pr.ProcessName == "FTPAutoIngest")
                    pr.Kill();
            }

            Process autoIngest;
            Process thumbNail;

            autoIngest = Process.Start(@"C:\Documents and Settings\TSS\Desktop\AssetAPP\FTPAutoRelease\FTPAutoIngest.exe");
            thumbNail = Process.Start(@"C:\Documents and Settings\TSS\Desktop\AssetAPP\VideoThumbNailRelease\ThumbnailVideo.exe");
        }
    }
}
